<template>
	<view class="ads-box">
		<block v-if="adType == 1">
			<image class="ad-bg" :src="option.adImg"></image>
			<view class="ad-1 time" v-if="isTime && time > 0">{{time}}</view>
			<view class="ad-1 skip" @click="close" v-else>跳过</view>
		</block>
		<block v-else>
			<view class="ad-up" :style="{'backgroundColor':option.bgColor}">
				<view class="ad-time-box grid col-2">
					<view class="ad-time-box-text">广告</view>
					<view class="ad-time-box-time">{{time + '秒'}}</view>
				</view>
				<view class="ad-mini-box" @click="toAds()">
					<view class="ad-mini-box-img" :style="{'background-image':'url('+option.adImg+')'}"></view>
					<view class="ad-mini-box-dec">{{option.adDesc}}</view>
					<view class="ad-mini-box-btn">{{option.adBtn}}</view>
				</view>
				<view class="ad-skip-box" @click="close">跳过</view>
			</view>
			<view class="ad-down">
				<view class="ad-down-logo" :style="{'background-image':'url('+logo+')'}"></view>
				<view class="ad-down-text">{{appName}}</view>
			</view>
		</block>
	</view>
</template>

<script>
	let timeout = null
	export default {
		name:"vshare-ads",
		props:{
			adType:{
				type:[String,Number],
				default:1
			},
			src:{
				type: String,
				default:'https://image.vshare.site/ads/2020ads.jpg'
			},
			isTime: {
				type: Boolean,
				default:true
			},
			logo:{
				type: String,
				default:'https://image.vshare.site/ads/2020ads.jpg'
			},
			appName: {
				type:String,
				default:'VSHARE'
			},
			option:{
				bgColor:'rgb(165, 103, 63,.8)',
				adAppid:'',
				adImg:'https://image.vshare.site/ads/2020ads.jpg',
				adDesc:'xxxxx',
				adBtn:'进入小程序',
				adType:1,
				adUrl:''
			}
		},
		data() {
			return {
				time: 3
			};
		},
		created() {
			var that = this;
			var times = setInterval(function(){
				that.time = parseInt(that.time);
				if(that.time > 0){
					that.time -= 1;
				}else{
					clearInterval(times);
					timeout = setTimeout(function(){
						that.close();
					},2000);
				}
			},1000);
		},
		methods:{
			toAds(){
				if(this.option.adUrl){
					switch(this.option.adType){
						case 1:
							uni.navigateTo({
								url: this.option.adUrl
							})
							break;
						default:
							uni.navigateToMiniProgram({
							  appId: this.option.adAppid,
							  path: this.option.adUrl,
							  extraData: {
							  },
							  success(res) {
							    // 打开成功
							  }
							})
							break;
					}
				}
			},
			close(){
				if(timeout != null){
					clearTimeout(timeout)
					timeout = null;
				}
				this.$emit('click')
			}
		}
	}
</script>

<style>
@import '/components/vshare-ads.css';
</style>
